using Microsoft.EntityFrameworkCore;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.EFCore;
using MinimalistFoodStreet.BackEnd.Server.Server.RequestExtend.Tables;
using MinimalistFoodStreet.BackEnd.Server.Server.ResponseExtend.Tables;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;

namespace MinimalistFoodStreet.BackEnd.Server.Server.Tables
{
    [InjectClass]
    public class ActionLogsServer : IActionLogsServer
    {
        private readonly ServerRepository _serverRepository;
        public ActionLogsServer(DbContext dbContext)
        {
            _serverRepository = new ServerRepository(dbContext);
        }

        public async Task<ResponseApiDto> AddAsync(RequestCreateActionLogsDto dto)
        {
            int affectedRows = await _serverRepository.actionLogsEFCore.AddAsync(dto.ToCreateEntity());
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> UpdateAsync(RequestUpdateActionLogsDto dto)
        {
            ActionLog actionLog = await _serverRepository.actionLogsEFCore.Query(d => d.AlId == dto.id).SingleOrDefaultAsync();
            if (actionLog == null)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            int affectedRows = await _serverRepository.actionLogsEFCore.UpdateAsync(actionLog);
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> DeleteAsync(RequestDeleteActionLogsDto dto)
        {
            int affectedRows = await _serverRepository.actionLogsEFCore.Query(d => d.AlId == dto.id).ExecuteDeleteAsync();
            if (affectedRows == 0)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> DetailAsync(int id)
        {
            ActionLog actionLog = await _serverRepository.actionLogsEFCore.Query(d => d.AlId == id).SingleOrDefaultAsync();
            if (actionLog == null)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            return ResponseApiDto.Ok("数据已获取", actionLog.ToResponseDto());
        }

        public async Task<ResponseApiDto> PaginationAsync(int page, int limit, string? key, DateTime? begain, DateTime? end, string? requestMethod)
        {
            int total = 0;
            List<ActionLog> actionLogs = await _serverRepository.actionLogsEFCore.Pagination(d => (key == null || d.AlAccount.AAccount.Contains(key)) && (begain == null || d.AlCreateTime >= begain) && (end == null || d.AlCreateTime <= end) && (requestMethod == null || d.AlRequestMethod.ToLower() == requestMethod.ToLower()), false, d => d.AlCreateTime, out total, page, limit).ToListAsync();
            List<ResponseActionLogsDto> responseActionLogsDtos = actionLogs.Select(d => d.ToResponseDto()).ToList();
            return ResponseApiDto.Ok("数据已获取", new { data = responseActionLogsDtos, total = total });
        }
    }
}